﻿using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using TomNet.Core.Options;
using TomNet.Exceptions;
using TomNet.Extensions;

namespace TomNet.Swagger
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddApiSwagger(this IServiceCollection services)
        {
            IConfiguration configuration = services.GetConfiguration();
            var options = configuration.GetTomNetOptions();

            if (!options.Swagger.Enabled)
            {
                return services;
            }

            string url = options.Swagger.Url;
            if (url.IsNullOrEmpty())
            {
                throw new TomNetException("配置文件中Swagger节点的Url不能为空");
            }

            string title = options.Swagger.Title;
            int version = options.Swagger.Version;

            //services.AddMvcCore().AddApiExplorer();
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc($"v{version}", new OpenApiInfo()
                {
                    Title = title,
                    Version = $"{version}",
                    //Description = "API说明文档",
                    //TermsOfService = new Uri("http://www.baidu.com"),
                    //Contact = new OpenApiContact
                    //{
                    //    Name = "TommyHeng",
                    //    Email = "39035290@qq.com",
                    //    //Url = new Uri("http://www.baidu.com")
                    //}
                });
                // 为 Swagger JSON and UI设置xml文档注释路径
                //需要配置项目--属性--生成--输出--XML文档 \bin\Debug\netcoreapp3.1\ApiDemo.xml
                //Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.xml").ToList().ForEach(file =>
                //{
                //    options.IncludeXmlComments(file, true);
                //});
                Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.xml").ToList().ForEach(file =>
                {
                    options.IncludeXmlComments(file);
                });

                //权限Token
                options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
                {
                    Description = "请输入带有Bearer的Token，形如 “Bearer {Token}” ",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                });
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
                        },
                        new[] { "readAccess", "writeAccess" }
                    }
                });
            });
            return services;
        }
    }
}
